﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Station.Common.Rejudgment
{
    /// <summary>
    /// 算子检测元件
    /// </summary>
    public class ErrComponent
    {
        /// <summary>
        /// 运动位置,像素坐标
        /// </summary>
        public Rectangle Position { get; set; } = new Rectangle();
        
        /// <summary>
        /// 区域角度
        /// </summary>
        public float Angle { get; set; }
        /// <summary>
        /// 图片
        /// </summary>
        public Bitmap Image { get; set; }

        /// <summary>
        /// 镜头中心
        /// </summary>
        Point CameraCenter { get; set; }
        /// <summary>
        /// 镜头图片
        /// </summary>
        public Bitmap CameraImage { get; set; }
        /// <summary>
        /// 检测Cell
        /// </summary>
        public List<ErrCell> ErrCells { get; set; } = new List<ErrCell>();
        
        public bool SetImage(Bitmap cameraImage,Point cameraCenter)
        {
            CameraImage = cameraImage;
            CameraCenter = cameraCenter;

            RectangleF rt = GetRect();
            RectangleF rect = new RectangleF(rt.Left - cameraCenter.X + 4096 / 2, rt.Top - cameraCenter.Y + 3000 / 2, rt.Width, rt.Height);
            Bitmap Image = (Bitmap)CameraImage.Clone(rect, PixelFormat.Format24bppRgb);
            if (Angle > 1.192092896e-07F && Image !=null)
            {
                int w = Position.Width;
                int h = Position.Height;
                Bitmap tempImg = new Bitmap(w, h, PixelFormat.Format24bppRgb);
                Graphics g = Graphics.FromImage(tempImg);
                g.TranslateTransform(w / 2.0f, h / 2.0f);
                g.RotateTransform(-Angle);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                g.DrawImage(Image, -Image.Width, -Image.Height);
                Image = tempImg;
            }
            return Image != null;
        }
        public RectangleF GetRect()
        {
            PointF[] point = new PointF[]
            {
                new PointF(Position.Left,Position.Bottom),
                new PointF(Position.Right,Position.Top),
                new PointF(Position.Right,Position.Bottom)
            };
            Matrix matrix = new Matrix();
            matrix.RotateAt(Angle, new PointF(Position.X, Position.Y),MatrixOrder.Append);
            matrix.TransformPoints(point);
            float l = Position.X;
            float r = Position.X;
            float t = Position.Y;
            float b = Position.Y;
            for (int i = 0; i < 3; i++)
            {
                if (l > point[i].X)
                    l = point[i].X;
                if (r < point[i].X)
                    r = point[i].X;
                if (t > point[i].Y)
                    t = point[i].Y;
                if (b < point[i].Y)
                    b = point[i].Y;
            }
            RectangleF rect=new RectangleF(l, t,(r-l),(b-t));
            return rect;
            
        }

    }
}
